package com.tubi.edu.info.manager.controller;

import com.tubi.edu.info.manager.domain.Teacher;
import com.tubi.edu.info.manager.service.TeacherService;

import java.util.Scanner;

//老师客服菜单
public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();
    public void start() {
        teacherLoop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用老师管理系统，再见！");
                    break teacherLoop; // 跳出循环
                default:
                    System.out.println("您输入的信息有误,请重新输入");
                    break;
            }
        }
    }
    //修改老师
    private void updateTeacher() {
        //从业务员中查询数据看看有没有录入信息
        Teacher[] teachers = teacherService.findAllTeacher();
        //判断数组中是否有元素
        if(teachers == null) {
            System.out.println("暂时没有信息，请添加后重试");
            return;
        }
        String id = inputTeacherId(); //输入id
        //接受老师其他信息
        Teacher t = inputTeacherInfo(id);

        teacherService.updateTeacher(id,t);
        System.out.println("修改成功");
    }

    //删除老师
    public void deleteTeacherById() {
        //从业务员中查询数据看看有没有录入信息
        Teacher[] teachers = teacherService.findAllTeacher();
        //判断数组中是否有元素
        if(teachers == null) {
            System.out.println("暂时没有信息，请添加后重试");
            return;
        }

        // 接受要删除的老师ID
        String id = inputTeacherId();
        teacherService.deleteStudentById( id );
        System.out.println("删除成功");
    }

    //查询老师
    public void findAllTeacher() {
        //从业务员中查询数据
        Teacher[] teachers = teacherService.findAllTeacher();
        //判断数组中是否有元素
        if(teachers == null) {
            System.out.println("暂时没有信息，请添加后重试");
            return;
        }

        //遍历打印
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher t  = teachers[i];
            if (t != null){
                System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "\t\t" + t.getBirthday());
            }
        }

    }

    //添加老师
    public void addTeacher() {
        String id;
        while (true){
            //键盘录入老师id
            System.out.println("请输入老师id");
             id = sc.next();
            //判断老师是否存在 给业务员
            boolean exists =  teacherService.isExists(id);
            //不存在添加 存在重新输入
            if(exists){
                System.out.println("id已经占用，重新输入");
            }else {
                break;
            }
        }
        //接受老师其他信息
        Teacher t = inputTeacherInfo(id);
        // 封装好的对象传递给teacherService 继续完成添加操作
        boolean result =  teacherService.addTeacher(t);
        if(result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }

    }
    //录入老师id
    public String inputTeacherId(){
        String id;
        while (true){
            //键盘录入老师id
            System.out.println("请输入老师id");
            id = sc.next();
            //判断老师是否存在 给业务员
            boolean exists =  teacherService.isExists(id);
            //不存在添加 存在重新输入
            if(!exists){
                System.out.println("录入的老师id不存在，请重新输入");
            }else {
                break;
            }
        }
        return id;
    }
    //录入老师信息 封装老师对象
    public Teacher inputTeacherInfo( String id){
        System.out.println("请输入老师姓名");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        String age = sc.next();
        System.out.println("请输入老师生日");
        String birthday = sc.next();

        //封装老师对象
        Teacher t = new Teacher();
        t.setId(id);
        t.setAge(age);
        t.setName(name);
        t.setBirthday(birthday);
        return t;
    }
}
